﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataAccessLayer;
using BusinessLogicLayer;
using System.IO;

namespace ChamCong.Admin
{
    public partial class PhanQuyenTrenTrang : BasePage
    {
        NhanVienBLL NVBLL = new NhanVienBLL();
        ROLE r = new ROLE();
        CT_ROLE ctr = new CT_ROLE();
        MenuBLL MBLL = new MenuBLL();
        PhanQuyenBLL pq = new PhanQuyenBLL();
        QuanLyPhanQuyenBLL q = new QuanLyPhanQuyenBLL();
        int _MaNV, _MaPB;
        public static string t;
        TreeViewState state;
        protected void Page_Load(object sender, EventArgs e)
        {
            t = Request.QueryString["t"];
            state = new TreeViewState("TreeViewState");
            _MaNV = Convert.ToInt32(Session["MaNV"]);
            _MaPB = NVBLL.TimPhongBan(_MaNV);

            if (!IsPostBack)
            {
                Page.SetFocus(tbxMoTa);
                HienThiMenu();

                if (t != null)
                {
                    rdbMenu.Items[0].Selected = true;
                    LoadData(t);
                    btnCapNhat.Visible = true;
                }
                else
                {
                    //rdbMenu.Items[1].Selected = true;

                }

                cblMaRole.DataSource = q.layTatCaRole();
                cblMaRole.DataValueField = "MaRole";
                cblMaRole.DataTextField = "TenRole";
                cblMaRole.DataBind();

                LoadData(t);
            }

                                   
        }
        void HienThiMenu()
        {
            string strHidden = string.Empty;
            List<MENU_TRAI> Cap1 = MBLL.LayDSMenuCapMot();

            foreach (var item in Cap1)
            {
                TreeNode child = new TreeNode(item.TenTrang + " (" + item.CapMot + ")", item.STT.ToString());
                //child.NavigateUrl = "PhanQuyenTrenTrang.aspx?t=" + item.DuongDan;
                child.ToolTip = item.TenTrang.ToString();
                MenuTrai.Nodes.Add(child);
                List<MENU_TRAI> Cap2 = MBLL.LayDSTatCaMenuCapHai((int)item.CapMot);
                foreach (var item2 in Cap2)
                {
                    if (item2.HienThi == 1)
                    {
                        strHidden = "";
                    }
                    else
                    {
                        strHidden = " - Ẩn";
                    }
                    TreeNode child2 = new TreeNode(item2.TenTrang + " (" + item2.CapHai + strHidden + ")", item2.STT.ToString());
                    child2.Value = "PhanQuyenTrenTrang.aspx?t=" + item2.DuongDan;
                    child2.ToolTip = item2.TenTrang.ToString();
                    child.ChildNodes.Add(child2);
                }
            }
            MenuTrai.CollapseAll();
            if (state.IsSaved)
            {
                state.RestoreTreeView(MenuTrai, "TreeViewState", HttpContext.Current);
            }
        }
        private void LoadData(string _DuongDan)
        {
            if (!string.IsNullOrEmpty(_DuongDan))
            {
                if (pq.layThongTinPhanQuyenTrenTrang(_DuongDan) != null)
                {
                    rdbMenu.Items[0].Selected = true;
                    tbxDuongDan.Text = pq.layThongTinPhanQuyenTrenTrang(_DuongDan).DuongDan;
                    tbxMoTa.Text = pq.layThongTinPhanQuyenTrenTrang(_DuongDan).MoTa;
                    List<int> temp = pq.layDSRoleTheoTenTrang(_DuongDan);
                    for (int i = 0; i < cblMaRole.Items.Count; i++)
                    {
                        if (temp.Contains(Convert.ToInt32(cblMaRole.Items[i].Value)))
                        {
                            cblMaRole.Items[i].Selected = true;
                        }
                    }
                }
                else
                {
                    if (MBLL.LayThongTinMenuItemTheoDuongDan(_DuongDan) != null)
                    {
                        MENU_TRAI m = new MENU_TRAI();
                        tbxDuongDan.Text = MBLL.LayThongTinMenuItemTheoDuongDan(_DuongDan).DuongDan;
                        tbxMoTa.Text = MBLL.LayThongTinMenuItemTheoDuongDan(_DuongDan).TenTrang;
                        sbcMsgBox4.SetTitleText(SBWebControls.sbcMsgBox.eMessageType.Warning, "Thông báo", "Hiện thời chưa có dữ liệu role trên trang!");
                    }
                    else
                    {
                        sbcMsgBox4.SetTitleText(SBWebControls.sbcMsgBox.eMessageType.Warning, "Thông báo", "Trang không tồn tại hoặc không có dữ liệu!");
                    }

                }

            }

        }

        private void setButton(bool _flag)
        {
            btnThem.Visible = _flag;
            btnCapNhat.Visible = !_flag;
            btnDel.Visible = _flag;
            
        }
        protected void btnThem_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(q.KiemTraTonTai(tbxTenRoleMoi.Text.Trim())))
            {
                if (tbxTenRoleMoi.Text != "")
                {
                    ROLE r = new ROLE();
                    //r.MaRole = Convert.ToInt32(tbxIDRole.Text); //ma role tu dong tang
                    r.TenRole = tbxTenRoleMoi.Text;
                    r.Xoa = 1;
                    q.themRole(r);
                    cblMaRole.DataSource = q.layTatCaRole();
                    cblMaRole.DataValueField = "MaRole";
                    cblMaRole.DataTextField = "TenRole";
                    cblMaRole.DataBind();
                    tbxTenRoleMoi.Text = "";
                    tbxTenRoleMoi.Focus();
                    sbcMsgBox4.SetTitleText(SBWebControls.sbcMsgBox.eMessageType.Success, "Thông báo", "Thêm role mới thành công!");
                }
                else
                {
                    sbcMsgBox4.SetTitleText(SBWebControls.sbcMsgBox.eMessageType.Error, "Thông báo", "Tên role mới không được để trống!");
                }

            }
            else
            {
                sbcMsgBox4.SetTitleText(SBWebControls.sbcMsgBox.eMessageType.Error, "Thông báo", "Role đã tồn tại!");
            }
        }

        protected void btnCapNhat_Click(object sender, EventArgs e)
        {
            try
            {
                //pq.xoaPhanQuyenTrenTrang(t);
                for (int i = 0; i < cblMaRole.Items.Count; i++)
                {
                    CT_PHANQUYEN ctq = new CT_PHANQUYEN();
                    ctq.DuongDan = tbxDuongDan.Text.ToLower();
                    ctq.MaRole = Convert.ToInt32(cblMaRole.Items[i].Value);
                    ctq.MoTa = tbxMoTa.Text;
                    pq.xoaQuyenTrenTrang(tbxDuongDan.Text, Convert.ToInt32(cblMaRole.Items[i].Value));
                    if (cblMaRole.Items[i].Selected)
                    {
                        //CT_PHANQUYEN ctq = new CT_PHANQUYEN();
                        ctq.DuongDan = tbxDuongDan.Text.ToLower();
                        ctq.MaRole = Convert.ToInt32(cblMaRole.Items[i].Value);
                        ctq.MoTa = tbxMoTa.Text;
                        pq.suaQuyenTrenTrang(ctq);
                        //sbcMsgBox4.SetTitleText(SBWebControls.sbcMsgBox.eMessageType.Success, "Thông báo", "Sửa role thành công!");
                    }
                    else
                    {

                    }
                }
                sbcMsgBox4.SetTitleText(SBWebControls.sbcMsgBox.eMessageType.Success, "Thông báo", "Cập nhật thành công!");
            }
            catch (Exception ex)
            {
                sbcMsgBox4.SetTitleText(SBWebControls.sbcMsgBox.eMessageType.Error, "Thông báo", ex.Message);
            }

        }

        protected void rdbMenu_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (rdbMenu.SelectedValue == "0") //Thêm role mới
            {
                pnlThongTin.Visible = false;
                pnlCheckBoxList2.Visible = true;
                foreach (ListItem li in cblMaRole.Items)
                {
                    li.Selected = false;
                }
                setButton(true);
            }
            else if (rdbMenu.SelectedValue == "2" && t != null) // Thông tin trang
            {
                pnlThongTin.Visible = true;
                pnlCheckBoxList2.Visible = false;
                LoadData(t);

                setButton(false);
            }
        }

        protected void btnDel_Click(object sender, EventArgs e)
        {
            try
            {
                ROLE r = new ROLE();
                for (int i = 0; i < cblMaRole.Items.Count; i++)
                {
                    if (cblMaRole.Items[i].Selected == true)
                    {
                        q.XoaRole(Convert.ToInt32(cblMaRole.Items[i].Value));
                    }
                }
                cblMaRole.DataSource = q.layTatCaRole();
                cblMaRole.DataValueField = "MaRole";
                cblMaRole.DataTextField = "TenRole";
                cblMaRole.DataBind();
                sbcMsgBox4.SetTitleText(SBWebControls.sbcMsgBox.eMessageType.Success, "Thông báo", "Xóa role đã chọn thành công!");
            }
            catch (Exception)
            {
                sbcMsgBox4.SetTitleText(SBWebControls.sbcMsgBox.eMessageType.Error, "Thông báo", "Không thể xóa role này!");
            }

        }

        protected void MenuTrai_SelectedNodeChanged(object sender, EventArgs e)
        {
            if (MenuTrai.SelectedNode.Depth == 1)
            {
                state.SaveTreeView(MenuTrai, "TreeViewState", HttpContext.Current);
                Response.Redirect(MenuTrai.SelectedNode.Value);
            }
        }

    }
}